package xyz.scootaloo.oj.offer

import org.junit.Test
import xyz.scootaloo.oj.ds.ListNode
import xyz.scootaloo.oj.ds.helper.CollectionHelper

/**
 * @author flutterdash@qq.com
 * @since 2021/4/9 18:11
 */
class P18 {

    private fun deleteNode(head: ListNode?, `val`: Int): ListNode? {
        if (head == null || head.`val` == `val`) return head?.next
        var preNode = head
        var rearNode = head.next
        while(rearNode != null && rearNode.`val` != `val`) {
            preNode = rearNode
            rearNode = rearNode.next
        }
        if (rearNode != null)
            preNode?.next = rearNode.next
        return head
    }

    @Test
    fun test0() {
        val node1 = ListNode(4)
        val node2 = ListNode(5)
        val node3 = ListNode(1)
        val node4 = ListNode(9)
        node1.next = node2
        node2.next = node3
        node3.next = node4
        CollectionHelper.showListNodes(node1)
        val head = deleteNode(node1, 9)
        CollectionHelper.showListNodes(head)
    }

}
